﻿ //--------------------------------
function $(id) {return document.getElementById(id); }

function IsIE() {return (document.all && window.external); }
 
//字符串转JSON对象
function String2JSON(strVal) {return eval('(' + strVal + ')'); }
 
//获取Url传过来的参数值
function GetUrlParm(paras)
 { 
    var url = location.href.toLowerCase();
    var paraString = url.substring(url.indexOf("?")+1,url.length).split("&");  
    var paraObj = {}  
    for (i=0; j=paraString[i]; i++)
    {  
        var iPos = j.indexOf("=");
        paraObj[j.substring(0,iPos)] = j.substring(iPos+1,j.length);  
    }  
    var returnValue = paraObj[paras];  
    if(typeof(returnValue)=="undefined") return "";  
    else return returnValue;
}

//-----------------xmlHttp---------------------
var xmlHttp;
var __on_ready_func;

//建立http对象
function CreateHttpObj()
{
	xmlHttp = null;
	if (window.XMLHttpRequest)
	{	xmlHttp = new XMLHttpRequest();	}	// code for IE7+, Firefox, Chrome, Opera, Safari
	else
	{
		try	{ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}// code for IE6, IE5
		catch(e){ xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}
	}
}

//往服务提交数据
function CreateXmlHttpPost(readyFunc, url, postData)
{
	 CreateHttpObj(); //重新建立http对象

	__on_ready_func = _DefaultReadyFunc;
	if(readyFunc != null) { __on_ready_func = readyFunc;}

	xmlHttp.onreadystatechange = on_ready_state;
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlHttp.send(postData);
}

//服务端返回数据处理方法
function on_ready_state()
{
	if(xmlHttp.readyState == 4)
	{
		if(xmlHttp.status ==200)	//成功
		{
		      //alert(xmlHttp.responseText);
			 __on_ready_func(xmlHttp.responseText); 
		}
		else
		{ _DeafultErrorShow("xmlHttp.status:" + xmlHttp.status);	}
	}
}

function _DeafultErrorShow(val){ alert(val);}

function _DefaultReadyFunc(val){ alert(val);}

 //获取结果集
function GetDataSet(jsonObj)
{     
    if(jsonObj == null) return null;
    if(jsonObj["success"] == false) 
    { 
        alert(jsonObj["error"]);
        
        var js = jsonObj["js"];//alert(js);
        if(js) eval(js); //执行重定向js 
        
        return null;
    } 
    
    return jsonObj["dataset"];
}

function GetSuccess(jsonObj){ return jsonObj["success"];}

function GetError(jsonObj){ return jsonObj["error"];}

function GetMessage(jsonObj){ return jsonObj["message"];}

//显示信息(xml对象， 成功执行的函数， 失败执行函数)
function ShowReturnMsg(jsonObj, callbackFunc)
{     
    if(jsonObj == null) return null;
    if(jsonObj["success"] == false) 
    {  
        alert(jsonObj["error"]);
    }  
    else  
    { 
        alert(jsonObj["message"]); 
        if(callbackFunc) callbackFunc(); 
    }
} 

//-------------------------post_php----------------------- 
var g_default_page_count = 15;      //查询表每页行数
var g_root_path = "../../";      //相对jsonRequest.php文件的路径 
var g_post_url = g_root_path + "jsonRequest.php";
//var g_post_url = g_root_path + "xmlRequest.php";

function PostToPHP(callBackFunc, command_page, command_type, postData)
{
	var _url = g_post_url;
	var _data = "command_page=" + command_page + "&command_type=" + command_type;
	if(postData != null & postData.length> 0){ _data += "&" + postData;}
   //alert(_data);
	CreateXmlHttpPost(callBackFunc, _url, _data);
}

//----------------------util---------------------------------------- 
//--------------------------------------------
var g_postfix = "swf";   //上传的文件的文件后缀

//---检查文件名后缀
function CheckFilePostfix_ext() 
{
	var len = arguments.length;
	for(var i=0; i<len; i++)
	{
		var obj = $(arguments[i]); 
		if(obj == null)  
            continue; 
        
        if(obj.disabled)  //对象禁用， 则不检查
            continue;
        
		if(!CheckFilePostfix(obj.value, g_postfix)) 
		{
			alert("检查文件名后缀失败...\n\n控件名:["+ obj.id +"]\n文件名:[" + obj.value + "]"); 
			return false;
		}		
	} 
	return true;
}

//-----检查文件名后缀(文件名，后缀名)
function CheckFilePostfix(fileName, postfix)
{ 
	if(fileName.indexOf(".") == -1) return false;
	
	var tempA = fileName.split("."); 
	if(tempA.length ==0) return false;
	
	return(tempA[tempA.length-1].toLowerCase() == postfix) 
}

//返回文件后缀
function GetFilePostfix()
{
    return g_postfix;
}

//--------------------------------------------------
//***********画<a> --(容器ID, 点击方法名, 方法参数, 显示文字)----- 
function AppendElementA(containID, funcName, funcParm, txtVal)
{
    containObj = $(containID);
    if(containObj == null) return;
    
	var aObj = document.createElement("a");
	aObj.href="javascript:"+ funcName +"('" + funcParm + "')"; 
    
	var tx = document.createTextNode(txtVal);
	aObj.appendChild(tx);
    
    //aObj.target = "_blank";     
	containObj.appendChild(aObj);
}
//***********画<a>2--(容器ID,显示文字)----
function AppendElementA2(containID, txtVal)
{
    containObj = $(containID);
    if(containObj == null) return;
    
	var aObj = document.createElement("a");
	aObj.href= txtVal; 
    
	var tx = document.createTextNode(txtVal.substr(txtVal.lastIndexOf('/')+1));
	aObj.appendChild(tx);
    
    aObj.target = "_blank";  
	containObj.appendChild(aObj);
}

//----------添加文本节点(容器ID,文字)
function AppendElementText(containID, txtVal)
{
    containObj = $(containID);
    if(containObj == null) return;  
    
	var tx = document.createTextNode(txtVal);
	containObj.appendChild(tx); 
}

//---填充select控件
function FillSelectObj(id, dsObj)
{
	var selectObj = $(id);
	if(selectObj == null || dsObj == null ) return;

	selectObj.options.length = 0	//清空
	for(var item in dsObj)
	{
		selectObj.options[selectObj.options.length] = new Option(dsObj[item], item);
	}
}

//---删除select控件，保留某个值
function DelSelectObjExt(id, extVal)
{
	var selectObj = $(id);
	if(selectObj == null) return;
	
	var tmpText = "";
	var bSuccess = false;
	var tmpLen = selectObj.options.length;  
	while(tmpLen > 0)
	{
		tmpLen--;
		tmpOption = selectObj.options[tmpLen];
		if(tmpOption && tmpOption.value == extVal)
		{
			tmpText = tmpOption.text;
			bSuccess = true;
			break;
		}
	}
	selectObj.options.length = 0	//清空
	if(bSuccess)
	{
		selectObj.options[selectObj.options.length] = new Option(tmpText, extVal);
	}
}


//---删除select控件，删除某个值
function DelSelectObjByVal(id, extVal)
{
	var selectObj = $(id);
	if(selectObj == null) return;
		
	var tmpTextArray = new Array();
	var tmpValueArray = new Array();
	
	var bSuccess = false;
	var tmpLen = selectObj.options.length;  
	while(tmpLen > 0)
	{
		tmpLen--;
		tmpOption = selectObj.options[tmpLen];
		if(tmpOption && tmpOption.value != extVal)
		{
			tmpTextArray.push(tmpOption.text);
			tmpValueArray.push(tmpOption.value);
		}
	}
	selectObj.options.length = 0	//清空
	tmpLen = tmpTextArray.length;
	while(tmpLen > 0)
	{
		tmpLen--;
		selectObj.options[selectObj.options.length] = new Option(tmpTextArray[tmpLen], tmpValueArray[tmpLen]);
	}
}

//----显示或隐藏对象(val = "none" 隐藏， val=""显示)
function DisplayObj(id, val)
{ 
    var obj = $(id);
    if(obj == null) return;
    obj.style.display = val;
} 

//----禁用对象(id, bool)
function DisabledObj(id, bVal)
{ 
    var obj = $(id);
    if(obj == null) return;
    obj.disabled = bVal;
}

//返回相对请求页面的路径
function GetSwfFileRootPath()
{
    return g_root_path; // ../../../
}

//返回默认分页表 每页行数
function GetDefaultPageCount()
{
    return g_default_page_count;
}
